package com.ts.api.module.api.convert.plan.context;

import com.gitee.apanlh.util.base.CollUtils;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.convert.core.handler.ApiConvertHandler;
import com.ts.api.module.api.convert.org.context.ApiOrgConvertContext;
import com.ts.api.module.api.convert.platform.context.ApiPlatformConvertContext;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import com.ts.api.module.product.service.ProductPlanTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  API计划转换器上下文-机构/平台
 *
 *  @author Pan
 */
@Component
public class ApiPlanConvertContext {

    @Autowired
    private ProductPlanTypeService productPlanTypeService;
    @Autowired
    private ApiOrgConvertContext apiOrgConvertContext;
    @Autowired
    private ApiPlatformConvertContext apiPlatformConvertContext;

    /**
     *  自动获取对接处理器，如果不存在则抛出
     *
     *  @param  productPO       主体实体
     *  @param  productPlanPO   主体计划实体
     *  @return ApiOrgSendEventListener
     */
    public ApiConvertHandler getPlanHandler(ProductPO productPO, ProductPlanPO productPlanPO) {
        //  机构计划
        if (productPlanTypeService.hasOrgPlan(productPlanPO)) {
            return apiOrgConvertContext.getHandler(productPO, productPlanPO);
        }
        //  平台计划或者平台特殊计划
        if (productPlanTypeService.hasSpecialPlan(productPlanPO) || productPlanTypeService.hasPlatformPlan(productPlanPO)) {
            return apiPlatformConvertContext.getHandler(productPO, productPlanPO);
        }
        throw new ApiMsgException(ApiEnum.API_NULL_FAIL.format("API对接-平台计划-无对应-计划类型", "null"));
    }

    /**
     *  获取所有计划已对接的key
     *
     *  @return List
     */
    public List<String> listConvertKey() {
        List<String> unionKeys = CollUtils.newArrayList();

        List<String> org = apiOrgConvertContext.listOrgKey();
        List<String> platform = apiPlatformConvertContext.listPlatformKey();

        unionKeys.addAll(org);
        unionKeys.addAll(platform);
        return unionKeys;
    }
}
